package com.raymond.client.connector;//package com.raymond.client.connector;
//
//import com.raymond.client.connector.AbstractClientConnector;
//import com.raymond.client.remoting.RemotingHelper;
//import com.raymond.client.session.AbstractClientSession;
//import io.netty.channel.ChannelHandlerContext;
//import io.netty.channel.ChannelInboundHandlerAdapter;
//import io.netty.channel.EventLoop;
//import io.netty.handler.timeout.IdleState;
//import io.netty.handler.timeout.IdleStateEvent;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.net.InetSocketAddress;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author :  raymond
// * @version :  V1.0
// * @date :  2022-11-03 14:01
// */
//public class SessionHandler extends ChannelInboundHandlerAdapter {
//
//    private final static Logger logger = LoggerFactory.getLogger(SessionHandler.class);
//
//    private AbstractClientSession session;
//
//    /** 当前重试次数 **/
//    private int retries = 0;
//
//
//    public SessionHandler(AbstractClientSession session) {
//        this.session = session;
//    }
//
//    @Override
//    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
//        logger.debug("Successfully established a connection to the server. remoteAddress:{}", remoteAddress);
//        retries = 0;
////            session = createSession(ctx);
////            sessionManager.addSession(session);
//        ctx.fireChannelActive();
//    }
//
//    @Override
//    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
//        final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
//        if (retries == 0) {
//            logger.debug("Lost the TCP connection with the server. remoteAddress:{}", remoteAddress);
//            ctx.close();
//        }
//        boolean allowRetry = session.getRetryPolicy().allowRetry(retries);
//        if (allowRetry) {
//            long sleepTimeMs = session.getRetryPolicy().getSleepTimeMs(retries);
//            logger.debug("Try to reconnect to the server after {}ms. Retry count: {}.", sleepTimeMs, ++retries);
//            final EventLoop eventLoop = ctx.channel().eventLoop();
//            eventLoop.schedule(() -> {
//                logger.debug("Reconnecting remoteAddress {} ... ", remoteAddress);
////                    InetSocketAddress socketAddress = RemotingHelper.string2SocketAddress(remoteAddress);
//                this.session.reConnect();
//            }, sleepTimeMs, TimeUnit.MILLISECONDS);
//        }
//        ctx.fireChannelInactive();
//    }
//
//    @Override
//    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
//        if (evt instanceof IdleStateEvent) {
//            IdleState state = ((IdleStateEvent) evt).state();
//            if (state == IdleState.ALL_IDLE) {
//                final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
//                logger.debug("发送心跳,remoteAddress:{},channelId:{}", remoteAddress, ctx.channel().id());
//                ctx.writeAndFlush(getHeartbeatRequest());
//            }
//        } else {
//            super.userEventTriggered(ctx, evt);
//        }
//    }
//}
